<?php
	require_once "gameAPI.php";
	
	function my_check_achievement($user_id, $job_title_id, $level_id)
	{
		$query_string = "SELECT achievement_id, description FROM Achievement_list 
							WHERE job_title_id = " . $job_title_id . " AND level_id <= " . $level_id . "
								AND achievement_id NOT IN (SELECT achievement_id FROM Achievement WHERE user_id = " . $user_id . ")";
		$result = mysql_query($query_string);
		
		if (! $result)
		{
			die("Error querying database @check_achievement : " . mysql_error());
		}
		while ($row = mysql_fetch_array($result))
		{
			echo $row["description"] . "<br />";
			my_insert_achievement_table($user_id, $row["achievement_id"]);
			my_post_achievement($user_id, $row["description"]);
		}
	}
	
	function my_insert_achievement_table($user_id, $achievement_id)
	{
		$query_string = "INSERT INTO Achievement(user_id, achievement_id) VALUES (". $user_id . " , " . $achievement_id . ")";
		mysql_query($query_string);
	}
	
	function my_post_achievement($user_id, $msg)
	{
		
		echo "<div id=\"fb-root\"></div>
						<script type=\"text/javascript\" src=\"http://connect.facebook.net/en_US/all.js\"></script>
						<script type=\"text/javascript\">
						FB.init({
						appId : '271211432894561',
						status : true, // check login status
						cookie : true, // enable cookies to allow the server to access the session
						xfbml : true, // parse XFBML
						oauth : false // disable OAuth since we're not ready for it
						});

						FB.ui(
						{
						method: 'feed',
						name: '".$msg."',
						link: 'apps.facebook.com/frenfactory',
						picture: 'http://107.20.229.155/frenfactory/images/frenfactory_icon.png',
						caption: 'Help your friend increase credits!',
						description: 'Unwilling to stay as a worker? Play FrenFactory and run your own factory!',
						to: '".$user_id."'
						},
						function(response) {
							if (response && response.post_id) {
								window.location.href='play.php';
							} else {
								window.location.href='play.php';								
							}
						}
						);
						</script>";
					
		//header("https://http://107.20.229.155/play.php");
	}
	
	
?>